

    let http = require('http');
    let fs = require('fs');
    let server = http.createServer();

    // let documentRoot = 'D:/nodeJSFileTest';
    let documentRoot = 'C:/Users/zhai/Desktop/实验项目';
    const hostname = 'http://127.0.0.1';
    const port = 3000;

    server.on('request', function(req, res) {

        let url = req.url;
        // 判断路径下文件是否存在 返回boolean值
        let exist = fs.existsSync(`${documentRoot}${url}`);
        console.log(`${documentRoot}${url}`);

        if(exist) {
            res.writeHead(200, {'content-type': 'text/html; charset="utf-8"'});
            fs.readdir(`${documentRoot}${url}`, function(err, files) {
                let content = '';
                if(url != '/') documentRoot = `${documentRoot}${url}`; 
                if(err) return res.end('文件打开错误');
                else {
                    for(let i=0;i<files.length;i++) {
                        let Root = `${documentRoot}/${files[i]}`;
                        if(fs.statSync(Root).isFile()) {
                            content += `
                                <span>文件：</span>
                                <a href="http://127.0.0.1:3000/${files[i]}">${files[i]}</a><br>
                            `;
                        }
                        if(fs.statSync(Root).isDirectory()) {
                            content += `
                                <span>目录：</span>
                                <a href="http://127.0.0.1:3000/${files[i]}">${files[i]}</a><br>
                            `;
                        }
                    }
                    res.end(content);
                }
            });
        }
        else {
            res.writeHead(200, {'content-type': 'text/html; charset="utf-8"'});
            res.end("localhost:3000"+url+" 该路径不存在");
        }

    });

    server.listen(port, function() {
        console.log(`Server is runing at ${hostname}:${port}`);
    });